package com.yanqu.road.dao.impl.activity.hsisland;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandBuffData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandPatronsData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class HsIslandUserDataDaoImpl extends TempDataObjectDao<HsIslandUserData> {

    public static String getHsIslandUserDataSql = "select * from `t_c_activity_hs_island_user_data` where `activityId`=? and `groupId`=?";
    public Map<Long, HsIslandUserData> getHsIslandUserMap(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Long, HsIslandUserData> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                HsIslandUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String updateHsIslandUserDataSql = "update `t_c_activity_hs_island_user_data` set serverId=?,unionId=?,power=?,imageId=?,unlockedImageIds=?,energy=?,energyRestoreTime=?,score=?,floorId=?,islandId=?,x=?,y=?,helpNum=?,helpNumCount=?,canPveInviteNum=?,canPveInviteNumCount=?,buffMap=?,patronsMap=?,battleFieldId=?,pvpEnergy=?,pvpEnergyLastTime=?,lastAttackMonsterTime=?,lastAttackPlayerTime=?,lastAttackBossTime=?,hadRewardBattleFieldIdList=?,activeTime=?,donateNum=?,dUsableDonateNum=?,createTime=? where activityId=? and groupId=? and userId=?";
    @Override
    protected boolean updateData(HsIslandUserData hsIslandUserData) {
        hsIslandUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getPower()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getImageId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandUserData.getUnlockedImageIds())));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getEnergyRestoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getIslandId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getX()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getY()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getHelpNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getHelpNumCount()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getCanPveInviteNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getCanPveInviteNumCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandUserData.getBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandUserData.getPatronsMap())));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getBattleFieldId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getPvpEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getPvpEnergyLastTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getLastAttackMonsterTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getLastAttackPlayerTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getLastAttackBossTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandUserData.getHadRewardBattleFieldIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getActiveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getDonateNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getDUsableDonateNum()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getUserId()));
        if (getDb().executeNoneQuery(updateHsIslandUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHsIslandUserDataSql = "insert into `t_c_activity_hs_island_user_data`(activityId,groupId,userId,serverId,unionId,power,imageId,unlockedImageIds,energy,energyRestoreTime,score,floorId,islandId,x,y,helpNum,helpNumCount,canPveInviteNum,canPveInviteNumCount,buffMap,patronsMap,battleFieldId,pvpEnergy,pvpEnergyLastTime,lastAttackMonsterTime,lastAttackPlayerTime,lastAttackBossTime,hadRewardBattleFieldIdList,activeTime,donateNum,dUsableDonateNum,createTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HsIslandUserData hsIslandUserData) {
        hsIslandUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getPower()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getImageId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandUserData.getUnlockedImageIds())));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getEnergyRestoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getIslandId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getX()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getY()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getHelpNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getHelpNumCount()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getCanPveInviteNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getCanPveInviteNumCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandUserData.getBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandUserData.getPatronsMap())));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getBattleFieldId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getPvpEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getPvpEnergyLastTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getLastAttackMonsterTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getLastAttackPlayerTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getLastAttackBossTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandUserData.getHadRewardBattleFieldIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getActiveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getDonateNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandUserData.getDUsableDonateNum()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandUserData.getCreateTime()));
        if (getDb().executeNoneQuery(insertHsIslandUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HsIslandUserData parseData(ResultSet rs) throws SQLException {
        HsIslandUserData hsIslandUserData = new HsIslandUserData();
        hsIslandUserData.setActivityId(rs.getInt("activityId"));
        hsIslandUserData.setGroupId(rs.getInt("groupId"));
        hsIslandUserData.setUserId(rs.getLong("userId"));
        hsIslandUserData.setServerId(rs.getLong("serverId"));
        hsIslandUserData.setUnionId(rs.getString("unionId"));
        hsIslandUserData.setPower(rs.getLong("power"));
        hsIslandUserData.setImageId(rs.getInt("imageId"));
        hsIslandUserData.setUnlockedImageIds(JSON.parseObject(rs.getString("unlockedImageIds"), Set.class));
        hsIslandUserData.setEnergy(rs.getInt("energy"));
        hsIslandUserData.setEnergyRestoreTime(rs.getLong("energyRestoreTime"));
        hsIslandUserData.setScore(rs.getLong("score"));
        hsIslandUserData.setFloorId(rs.getInt("floorId"));
        hsIslandUserData.setIslandId(rs.getInt("islandId"));
        hsIslandUserData.setX(rs.getInt("x"));
        hsIslandUserData.setY(rs.getInt("y"));
        hsIslandUserData.setHelpNum(rs.getInt("helpNum"));
        hsIslandUserData.setHelpNumCount(rs.getInt("helpNumCount"));
        hsIslandUserData.setCanPveInviteNum(rs.getInt("canPveInviteNum"));
        hsIslandUserData.setCanPveInviteNumCount(rs.getInt("canPveInviteNumCount"));
        hsIslandUserData.setBuffMap(JSON.parseObject(rs.getString("buffMap"), new TypeReference<Map<Integer, HsIslandBuffData>>() {}));
        hsIslandUserData.setPatronsMap(JSON.parseObject(rs.getString("patronsMap"), new TypeReference<Map<Integer, HsIslandPatronsData>>() {}));
        hsIslandUserData.setBattleFieldId(rs.getLong("battleFieldId"));
        hsIslandUserData.setPvpEnergy(rs.getLong("pvpEnergy"));
        hsIslandUserData.setPvpEnergyLastTime(rs.getLong("pvpEnergyLastTime"));
        hsIslandUserData.setLastAttackMonsterTime(rs.getLong("lastAttackMonsterTime"));
        hsIslandUserData.setLastAttackPlayerTime(rs.getLong("lastAttackPlayerTime"));
        hsIslandUserData.setLastAttackBossTime(rs.getLong("lastAttackBossTime"));
        hsIslandUserData.setActiveTime(rs.getLong("activeTime"));
        hsIslandUserData.setDonateNum(rs.getLong("donateNum"));
        hsIslandUserData.setDUsableDonateNum(rs.getInt("dUsableDonateNum"));
        hsIslandUserData.setCreateTime(rs.getLong("createTime"));
        String hadRewardBattleFieldIdList = rs.getString("hadRewardBattleFieldIdList");
        if (!StringUtils.isNullOrEmpty(hadRewardBattleFieldIdList)) {
            hsIslandUserData.setHadRewardBattleFieldIdList(JSON.parseObject(hadRewardBattleFieldIdList, new TypeReference<List<Long>>() {
            }));
        }
        return hsIslandUserData;
    }
}
